package com.aidong.coupons.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.coupons.biz.MstCouponsBiz;
import com.aidong.coupons.entity.MstCoupons;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class MstCouponsAction implements ModelDriven<MstCoupons>{
	
	private Map<String, Object> map; // 返回的json数据
	private MstCoupons mstCoupons;//代金券实体类
	private MstCouponsBiz mstCouponsBiz;//代金券service
	
	
	@Override
	public MstCoupons getModel() {
		// TODO Auto-generated method stub
		if (mstCoupons == null) {
			mstCoupons = new MstCoupons();
		}
		return mstCoupons;
	}


	@Autowired
	public void setMstCouponsBiz(MstCouponsBiz mstCouponsBiz) {
		this.mstCouponsBiz = mstCouponsBiz;
	}

	/**
	 * 返回的json数据
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	@Action(value="couponsusable",results={@Result(name="succ",type="json",params={"root","map"})})
	public String couponsUsable() {
		map = new HashMap<String, Object>();
		String couponsNum = mstCoupons.getCouponsNum();
		
		if (null != couponsNum) {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("couponsNum",couponsNum.toUpperCase());
			
			Map<String, Object> resultMap = new HashMap<String,Object>();
			resultMap = mstCouponsBiz.couponsUsable(paraMap);
			
			if (resultMap == null) {
				map.put("code", 0);
				map.put("msg", "代金券无效");
			}else {
				//判断代金券是否过期
				String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				String validDateBegin = resultMap.get("validDateBegin").toString();
				String validDateEnd = resultMap.get("validDateEnd").toString();
				String couponsPrice = resultMap.get("couponsPrice").toString();
				if (date.compareTo(validDateBegin) >= 0 && date.compareTo(validDateEnd) <= 0) {
					map.put("code", 1);
					map.put("result", couponsPrice);
				}else {
					map.put("code", 0);
					map.put("msg", "代金券过期");
				}
				
			}
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			
		}
		return "succ";
	}
	
	/**
	 * 券冲正接口
	 * @return
	 */
	@Action(value="couponsreverse",results={@Result(name="succ",type="json",params={"root","map"})})
	public String couponsReverse() {
		map = new HashMap<String, Object>();
		String couponsNum = mstCoupons.getCouponsNum();
		int orderId = mstCoupons.getOrderId();
		if (null != couponsNum && 0 != orderId) {
			//冲正需要满足的条件
			//订单号一致 且 状态为2  且使用人0
			Map<String, Object> paraMap = new HashMap<String, Object>();
			//paraMap.put("couponsNum",couponsNum.toUpperCase());
			paraMap.put("orderId",orderId);
			int count = mstCouponsBiz.selectCouponsReverse(paraMap);
			if (count != 0) {
				//冲正
				mstCouponsBiz.setCouponsCanUse(paraMap);	
			}
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
			
		}
		return "succ";
	}
}
